<script setup lang="ts">
import { onMounted, ref } from 'vue'

// 1. 声明ref关联变量
const pRef = ref(null)

// 在onMounted中可以最早的获取到dom元素
onMounted(() => {
  // 3. 获取ref
  console.log(pRef.value)
  // 4. 可以通过document.querySelector()获取元素类型，并将pRef.value强制转换成相应的类型，从而获得代码提示
  // const p = document.querySelector('p')
  ;(pRef.value! as HTMLParagraphElement).style.color = 'red'
})
</script>

<template>
  <div>
    <!-- 2. 关联ref -->
    <p ref="pRef">炒饭</p>
  </div>
</template>

<style>
* {
  margin: 0;
  padding: 0;
  list-style: none;
}

.news {
  display: flex;
  height: 120px;
  width: 600px;
  margin: 0 auto;
  padding: 20px 0;
  cursor: pointer;
}

.news .left {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding-right: 10px;
}

.news .left .title {
  font-size: 20px;
}

.news .left .info {
  color: #999999;
}

.news .left .info span {
  margin-right: 20px;
}

.news .right {
  width: 160px;
  height: 120px;
}

.news .right img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style>
